package com.play.service.impl;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.play.common.utils.SnowFlakeUtil;
import com.play.entity.Herb;
import com.play.mapper.HerbMapper;
import com.play.service.IHerbService;

@Service
public class HerbServiceImpl implements IHerbService {
	
	@Autowired
	private HerbMapper herbMapper;

	@Override
	public int addHerb(Herb herb) {
		if (StringUtils.isBlank(herb.getName())) {
			return 0;
		}
		
		Herb h = selectHerbByName(herb.getName());
		if (null != h) {
			return 0;
		}
		herb.setId(SnowFlakeUtil.getId());
		herb.setCdate(new Date());
		herb.setMdate(new Date());
		return herbMapper.insert(herb);
	}
	
	@Override
	public PageInfo<Herb> selectHerbList(int pageNum, int pageSize, String name, String description) {
		PageHelper.startPage(pageNum, pageSize);
		PageInfo<Herb> pageInfo = new PageInfo<>(herbMapper.selectHerbList(name, description));
		return pageInfo;
	}
	
	@Override
	public Herb selectHerbByName(String name) {
		return herbMapper.selectHerbByName(name);
	}
	
	@Override
	public int updateHerb(Herb herb) {
		herb.setMdate(new Date());
		return herbMapper.updateByPrimaryKeySelective(herb);
	}

	@Override
	public Herb selectHerbById(Long id) {
		return herbMapper.selectByPrimaryKey(id);
	}

}
